package com.che.libcommon.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.TextViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.che.common.libcommon.R;
import com.che.libcommon.ui.VOMessage;

/**
 * Created by zl on 2017/12/21.
 */

public class LoadingLayout extends FrameLayout implements OnClickListener{

  public LoadingLayout(@NonNull Context context) {
    this(context,null);
  }

  public LoadingLayout(@NonNull Context context,
      @Nullable AttributeSet attrs) {
    this(context, attrs,0);

  }

  public LoadingLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);


    inflate(context, R.layout.common_lib_layout_loading,this);


    errorView = findViewById(R.id.loading_loader_message_layout);
    loadingView = findViewById(R.id.loading_progress_layout);


    imgIcon = errorView.findViewById(R.id.loading_img_icon);
    tvMsgTitle = errorView.findViewById(R.id.loading_tv_error_msg);
    tvMsgTo = errorView.findViewById(R.id.loading_tv_error_msg_to);
    tvBtn = errorView.findViewById(R.id.loading_btn_error_retry);
    tvBtn.setOnClickListener(this);
  }


  private View errorView;
  private View loadingView;
  private View contentView;


  private ImageView imgIcon;
  private TextView tvMsgTitle;
  private TextView tvMsgTo;
  private TextView tvBtn;

  private OnClickListener mOnRetryClickListener;
  private OnClickListener mOnEmptyClickListener;

  private VOMessage currentMessage;

  @Override
  protected void onFinishInflate() {
    super.onFinishInflate();
    showLoading(true);
  }


  public void showLoading(boolean isShow) {
    hideViews(errorView,contentView);

    loadingView.setVisibility(isShow?VISIBLE:GONE);

  }


  public void showError(VOMessage voError) {
    hideViews(loadingView,contentView,errorView);
    errorView.setVisibility(VISIBLE);
    if(voError == null){
      throw new NullPointerException("please override getEmptyMessage() method !!!");
    }

    currentMessage = voError;

    if(voError.drawableRes != 0){
      imgIcon.setVisibility(VISIBLE);
      imgIcon.setImageResource(voError.drawableRes);
    }else {
      imgIcon.setVisibility(GONE);
    }


    if(voError.titleRes != 0){
      tvMsgTitle.setVisibility(VISIBLE);
      tvMsgTitle.setText(voError.titleRes);
    }else {
      tvMsgTitle.setVisibility(GONE);
    }

    if(voError.describeRes != 0){
      tvMsgTo.setVisibility(VISIBLE);
      tvMsgTo.setText(voError.describeRes);
    }else {
      tvMsgTo.setVisibility(GONE);
    }

    if(voError.btnRes != 0){
      tvBtn.setVisibility(VISIBLE);
      tvBtn.setText(voError.btnRes);
      if(voError.type==VOMessage.TYPE_NETWORK_ERROR){
        tvBtn.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.refresh_btn_bg));
      }else{
        tvBtn.setBackground(null);
      }
    }else {
      tvBtn.setVisibility(GONE);
    }


  }


  public void showData() {
    currentMessage = null;
    hideViews(loadingView,errorView);
    if(contentView != null){
      contentView.setVisibility(VISIBLE);
    }
  }


  public void setContentView(View contentView) {
    hideViews(contentView);
    this.contentView = contentView;
    addViewInLayout(contentView,-1,new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
  }

  public void setOnRetryClickListener(OnClickListener onRetryCallback) {
    this.mOnRetryClickListener = onRetryCallback;
  }

  public void setOnEmptyClickListener(OnClickListener clickListener){
    this.mOnEmptyClickListener = clickListener;
  }

  private void hideViews(View... views){
    for (View view : views) {
      if(view != null){
        view.setVisibility(GONE);
      }
    }
  }

  @Override
  public void onClick(View v) {
    int id = v.getId();
    if(id == R.id.loading_btn_error_retry){

      if(currentMessage != null){
        if(currentMessage.type == VOMessage.TYPE_EMPTY_DATA){
          if(mOnEmptyClickListener != null){
            mOnEmptyClickListener.onClick(v);
          }
        }else {
          if(mOnRetryClickListener != null){
            mOnRetryClickListener.onClick(v);
          }
        }
      }


    }
  }


  public void setTextMsgStyle(@StyleRes int tvMsgTitleResId,@StyleRes int tvMsgToResId){
    if(tvMsgTitleResId != 0){
      TextViewCompat.setTextAppearance(tvMsgTitle,tvMsgTitleResId);
    }

    if(tvMsgToResId != 0){
      TextViewCompat.setTextAppearance(tvMsgTo,tvMsgToResId);
    }
  }

}
